﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.Documents.Repository;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Documents
{
    public class DocumentJournalRelationshipRepository : EfCoreRepository<FinanceDbContext, DocumentJournalRelationship , Guid>, IDocumentJournalRelationshipRepository
    {
        public DocumentJournalRelationshipRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        public Task<DocumentJournalRelationship> FindAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            return DbContext.Set<DocumentJournalRelationship>().AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<List<DocumentJournalRelationship>> FindAsync(DocumentJournalQuery query)
        {
            var relationships = await DbContext.Set<DocumentJournalRelationship>().AsNoTracking()
                .WhereIf(query.Id.HasValue, x => x.Id == query.Id)
                .WhereIf(query.Ids != null, x => query.Ids.Contains(x.Id))
                .WhereIf(query.DocumentId.HasValue, x => x.DocumentId == query.DocumentId)
                .WhereIf(query.JournalId.HasValue, x => x.JournalId == query.JournalId)
                .ToListAsync();

            return relationships;
        }
    }
}
